//
//  BGOwnAppraiseListController.m
//  bigMall
//
//  Created by LTOVE on 16/9/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGOwnAppraiseListController.h"
#import "BGOwnAppraiseListCell.h"
#import "BGOwmAppraiseList.h"
#import "BGDateResult.h"
#import "BGBuyerListDetailController.h"

#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

@interface BGOwnAppraiseListController (){
    
    NSMutableArray *dataArray;
    NSInteger _thisPage;
    NSInteger _allPage;
}

@end

@implementation BGOwnAppraiseListController

- (instancetype)init
{
    if (self = [super init]) {
        _thisPage = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.header beginRefreshing];
    self.tableView.backgroundColor = dockwhite_;
    
}

- (void)loadData
{
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult ownAppraiseListWithThisPage:@"1" pageNum:@"10" success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        BGOwmAppraiseList *resultModel = (BGOwmAppraiseList *)result;
        dataArray = [NSMutableArray arrayWithArray:resultModel.list];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        if ([resultModel.allPage integerValue] > _thisPage) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            _thisPage = 2;
            _allPage = [resultModel.allPage integerValue];
        }
        
    } failure:^(NSError *errot) {
        
        [MBProgressHUD hideHUD];
    }];
}

- (void)loadMoreData
{
    if (_thisPage > _allPage) {
        [self.tableView.footer endRefreshingWithNoMoreData];
        return;
    }
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult ownAppraiseListWithThisPage:[NSString stringWithFormat:@"%ld",(unsigned long)_thisPage] pageNum:@"10" success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        BGOwmAppraiseList *resultModel = (BGOwmAppraiseList *)result;
        [self.tableView.footer endRefreshing];
        [dataArray addObjectsFromArray:resultModel.list];
        [self.tableView reloadData];
        if (resultModel.list.count) {
            _thisPage++;
        }
        
    } failure:^(NSError *errot) {
        [MBProgressHUD hideHUD];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BGOwnAppraiseListCell *cell = [BGOwnAppraiseListCell cellWithTableView:tableView];
    BGOwmAppraiseListRes *model = dataArray[indexPath.row];
//    model.cellHeigh = 80;
    cell.model = model;
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BGBuyerListDetailController *vc = [[BGBuyerListDetailController alloc]init];
    //    UIBarButtonItem *bacItem = [[UIBarButtonItem alloc]init];
    //    bacItem.title = @"";
    //    self.navigationItem.backBarButtonItem = bacItem;
//    BGShellerOrderListListAddayModel *model = _dataArray[indexPath.row];
    BGOwmAppraiseListRes *model = dataArray[indexPath.row];
    vc.orderId = model.order_id;
    vc.comStr = model.content;
    vc.oneStr = model.score_1;
    vc.twoStr = model.score_2;
    vc.threeStr = model.score_3;
    vc.title = @"订单详情";
    UIBarButtonItem *bac = [[UIBarButtonItem alloc]init];
    bac.title = @"";
    vc.navigationItem.backBarButtonItem = bac;
    //    [vc.navigationItem.backBarButtonItem setTitle:@"sss"];
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
